/*
-----------------------------------------------------------------------------
This source file is part of as4game
    (ActionScript For Game Engine)
For the latest info, see http://as4game.googlecode.com

Copyright (c) 2000-2011 Hproof Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DRenderMode;
	import flash.display3D.Context3DStencilAction;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.Program3D;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import org.as4game.common.math.Math2;
	import org.as4game.common.utils.Fps;
	import org.as4game.render3d.Render3D;
	import org.as4game.resource.base.Resource;
	import org.as4game.resource.base.VertexBuffer;
	import org.as4game.resource.manager.ResourceManager;
	import org.as4game.resource.material.Material;
	import org.as4game.resource.mesh.Mesh;
	import org.as4game.resource.skeleton.Skeleton;
	import org.as4game.scene3d.Camera;
	import org.as4game.scene3d.Entity;
	import org.aswing.AsWingManager;
	import org.aswing.AssetPane;
	import org.aswing.JFrame;
	import org.aswing.JPopup;
	import org.aswing.JRootPane;
	import org.aswing.JWindow;
	import org.aswing.UIManager;
	import org.aswing.plaf.basic.border.FrameBorder;
	import org.ext.loader.ogre.MeshSerializer;
	import org.ext.loader.ogre.SkeletonSerializer;
	
	/**
	 * TestLoader
	 */
	public class TestLoader extends Sprite
	{
		// 资源 ID 定义
		static private const ID_MESH_SINBAD:String = "sinbad";
		static private const ID_MESH_SWORD:String = "sword";
		static private const ID_SKELETON_SINBAD:String = "sinbad";
		static private const ID_MATERIAL_BODY:String = "body";
		static private const ID_MATERIAL_CLOTHES:String = "clothes";
		static private const ID_MATERIAL_SWORD:String = "sword";
		
		// 骨骼/材质 名字->ID 映射
		static private const nameMap:Object = {};
		{
			nameMap[ "Sinbad.skeleton" ] 		= ID_SKELETON_SINBAD;
			nameMap[ "Sinbad/Eyes" ] 			= ID_MATERIAL_BODY;
			nameMap[ "Sinbad/Body" ] 			= ID_MATERIAL_BODY;
			nameMap[ "Sinbad/Gold" ] 			= ID_MATERIAL_BODY;
			nameMap[ "Sinbad/Teeth" ] 		= ID_MATERIAL_BODY;
			nameMap[ "Sinbad/Sheaths" ] 		= ID_MATERIAL_SWORD;
			nameMap[ "Sinbad/Spikes" ] 		= ID_MATERIAL_CLOTHES;
			nameMap[ "Sinbad/Clothes" ] 		= ID_MATERIAL_CLOTHES;
		}
		
		public function TestLoader()
		{
//			test_ogre_mesh();
//			test_ogre_skeleton();
//			test_ogre_material();
//			test_entity();
			test_render();
		}
		
		[Embed(source="../res/Sinbad.mesh", mimeType="application/octet-stream")]
		static public const SinbadMeshClass:Class;
		[Embed(source="../res/Sword.mesh", mimeType="application/octet-stream")]
		static public const SwordMeshClass:Class;
		
		private function test_ogre_mesh():void{
			
			var ids:Array = [ ID_MESH_SINBAD, ID_MESH_SWORD ];
			var classes:Array = [ SinbadMeshClass, SwordMeshClass ];
			for(var i:int=0; i<ids.length; i++){
				if( !ResourceManager.getMesh( ids[i] ) ){
					var ba:ByteArray = new classes[i] as ByteArray;
					var mesh:Mesh = MeshSerializer.importMesh( ba );
					mesh.bindSkeleton( nameMap );
					ResourceManager.setMesh( ids[i], mesh );
				}
			}
		}
		
		
		[Embed(source="../res/Sinbad.skeleton", mimeType="application/octet-stream")]
		static public const SinbadSkeletonClass:Class;
		
		private function test_ogre_skeleton():void{
			
			if( ! ResourceManager.getSkeleton( ID_SKELETON_SINBAD ) ){
				var ba:ByteArray = new SinbadSkeletonClass as ByteArray;
				var skeleton:Skeleton = SkeletonSerializer.importSkeleton( ba );
				ResourceManager.setSkeleton( ID_SKELETON_SINBAD, skeleton );
			}
		}
		
		[Embed( source = "../res/sinbad_body.jpg" )]
		protected const txt_body_class:Class;
		[Embed( source = "../res/sinbad_clothes.jpg" )]
		protected const txt_clothes_class:Class;
		[Embed( source = "../res/sinbad_sword.jpg" )]
		protected const txt_sword_class:Class;
		
		private function test_ogre_material():void{
			
			var ids:Array = [ ID_MATERIAL_BODY, ID_MATERIAL_CLOTHES, ID_MATERIAL_SWORD ];
			var classes:Array = [ txt_body_class, txt_clothes_class, txt_sword_class ];
			for(var i:int=0; i<ids.length; i++){
				if( !ResourceManager.getMaterial( ids[i] ) ){
					var mat:Material = Material.fromBitmap( (new classes[i]).bitmapData );
					ResourceManager.setMaterial( ids[i], mat );
					mat.rtt = true;
				}
			}
		}
		
		// 加载所有资源
		private function load_resource():void{
			test_ogre_skeleton();
			test_ogre_material();
			test_ogre_mesh();	
		}
		
		private function test_entity():void{
			load_resource();
			
			var ent1:Entity = new Entity( "Sinbad", ID_MESH_SINBAD);
			ent1.bindMaterial( nameMap );
			
			var ent2:Entity = new Entity( "Sword", ID_MESH_SWORD);
			ent2.setMaterial( ID_MATERIAL_SWORD );
		}
		

		//
		private var render3d:Render3D;
		private var context3d:Context3D;
		private var info:TextField;
		private var fps:Fps;
		private var frame:JFrame;
		private var bitmap:BitmapData;
		private var entity:Entity;
		private var camera:Camera;
		private function test_render():void{
			load_resource();
			entity = new Entity( "Sinbad", ID_MESH_SINBAD);
			entity.bindMaterial( nameMap );
			
			camera = new Camera();
			
			// ui
			info = new TextField;
			info.autoSize = "left";
			info.mouseEnabled = false;
			addChild(info);
			
			fps = new Fps();
			
			AsWingManager.initAsStandard( this );
			
			var bmp:Bitmap = new Bitmap(null);
			bitmap = bmp.bitmapData = new BitmapData(400, 300, true, 0);
			
			frame = new JFrame(this, "Demo");
			frame.setResizable( false );
//			frame.setBorder( new FrameBorder() );
//			frame.setOpaque( false );
			frame.getContentPane().append( new AssetPane( bmp ) );
			frame.pack();
			frame.show();
			
//			graphics.beginFill(0x003300);
//			graphics.drawRect(0, 0, 600, 400);
//			graphics.endFill();
			
			//
			render3d = new Render3D( stage, onContext3DCreate );
		}
		private function onContext3DCreate(render3d:Render3D):void{
			render3d.viewport = new Rectangle(0, 0, bitmap.width, bitmap.height);
			context3d = render3d.context3D;
			context3d.configureBackBuffer( bitmap.width, bitmap.height, 1);
			
			stage.addEventListener( Event.ENTER_FRAME, onEnterFrame);
		}
		private function onEnterFrame(e:Event):void{
			
			fps.update();
			info.htmlText = "<font color='#00ff00'>FPS: " + fps.fps + "</font>";
			
			entity.updateAnimation();
			
			context3d.clear();
//			context3d.drawToBitmapData( bitmap );
			context3d.present();
		}
	}
}